//
// Created by liushuai on 2021/1/28.
//

#include "maxWater.h"

long long maxWater(vector<int> &arr) {
    if (arr.size() <= 2)
        return 0;
    int l = 0, r = arr.size() - 1;
    long long area = 0;
    int min_value = min(arr[l], arr[r]);
    while (l < r) {
        if (arr[l] < arr[r]) {
            l++;
            if (arr[l] < min_value) {
                area += min_value - arr[l];
            } else {
                min_value = min(arr[l], arr[r]);
            }
        } else {
            r--;
            if (arr[r] < min_value) {
                area += min_value - arr[r];
            } else {
                min_value = min(arr[r], arr[l]);
            }
        }
    }
    return area;
}